309. 买卖股票的最佳时机含冷冻期
为保证权益,题目请参考 309. 买卖股票的最佳时机含冷冻期(From LeetCode).
解决方案1
CPP
C++
/*
309. 最佳买卖股票时机含冷冻期
六个股票买卖问题
*/
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
using namespace std;
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
if(prices.size()==0)
{
return 0;
}
int dp[100000][3][2] = {0};
dp[0][0][0] = 0;
dp[0][0][1] = 0;
for (int i = 0; i < prices.size(); ++i)
{
for (int k = 1; k <= 2; ++k)
{
if (i == 0)
{
dp[i][0][0] = 0;
dp[i][0][1] = INT_MIN;
dp[i][1][0] = 0;
dp[i][1][1] = -prices[i];
dp[i][2][0] = 0;
dp[i][2][1] = INT_MIN;
break;
}
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]);
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]);
}
}
return max(dp[prices.size() - 1][2][0], dp[prices.size() - 1][1][0]);
}
};
int main()
{
vector<int> vec;
// vec.push_back(1);
// vec.push_back(2);
// vec.push_back(3);
// vec.push_back(4);
// vec.push_back(5);
vec.push_back(3);
vec.push_back(3);
vec.push_back(5);
vec.push_back(0);
vec.push_back(0);
vec.push_back(3);
vec.push_back(1);
vec.push_back(4);
Solution so;
cout << so.maxProfit(vec);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70